﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using mal.balades.Business.DataModel;
using System.Data.Linq;

namespace mal.balades.Business.DataAccess
{
    public class VisitDataAccess : BaseDataAccess
    {
        public VisitDataAccess(BaladesDataContext dataContext)
            : base(dataContext)
        {
            DataLoadOptions loadOptions = new DataLoadOptions();
            loadOptions.LoadWith<Visit>(v => v.Pictures);
            loadOptions.LoadWith<Visit>(v => v.Informations);

            DataContext.LoadOptions = loadOptions;
        }

        public IEnumerable<VisitList> GetAllVisits()
        {
            IEnumerable<VisitList> results = DataContext.GetAllVisits();

            return results;
        }

        public Visit GetVisit(string visitUrlName)
        {
            var result = from v in DataContext.Visits
                         where string.Compare(v.UrlName, visitUrlName, true) == 0
                         select v;

            return result.SingleOrDefault();
        }

        public Visit GetVisit(int visitId)
        {
            var result = from v in DataContext.Visits
                         where v.VisitID == visitId
                         select v;

            Visit visit = result.SingleOrDefault();

            return visit;
        }

        public void Update(Visit visit)
        {
            Visit source = this.GetVisit(visit.VisitID);

            if (source == null)
                Insert(visit);
            else
                Update(source, visit);

            this.DataContext.SubmitChanges();

            IEnumerable<Visit> visits = this.DataContext.Visits.Where(v => v.Title == visit.Title);

            visit.VisitID = visits.First().VisitID;
        }

        private void Insert(Visit visit)
        {
            visit.CreationDate = DateTime.Now;
            this.DataContext.Visits.InsertOnSubmit(visit);
        }

        private void Update(Visit source, Visit destination)
        {
            source.Latitude = destination.Latitude;
            source.Longitude = destination.Longitude;
            source.MarketingCategoryID = destination.MarketingCategoryID;
            source.PlaceCategoryID = destination.PlaceCategoryID;
            source.SumUp = destination.SumUp;
            source.Text = destination.Text;
            source.Title = destination.Title;
            source.UrlName = destination.UrlName;
            source.Visible = destination.Visible;

            foreach (Picture picture in destination.Pictures)
            {
                if (picture.PictureID == 0)
                {
                    source.Pictures.Add(picture);
                }
                else
                {
                    Picture pictureSource = source.Pictures.Where(p => p.PictureID == picture.PictureID).Single();

                    pictureSource.PictureTypeID = picture.PictureTypeID;
                    pictureSource.Rank = picture.Rank;
                    pictureSource.Title = picture.Title;
                    pictureSource.Url = picture.Url;
                }
            }
        }
    }
}
